<?php
namespace App\Services;
use Cache,Log;
use Illuminate\Support\Facades\Redis;

class RedisService
{
	 

	public static function set($key,$data,$timestamp='')
	{
		
		try {
			Redis::set($key, $data);
			if($timestamp) {
				Redis::expireat($key,$timestamp);
			}		
		} catch(\Exception $e) {
			Log::error($e->getMessage());
		}
		return true;
			
	}

	public static function get($key)
	{
		return Redis::get($key);
	}

	

	public static function del($key)
	{
		return Redis::del($key);
	}
	
	public static function incr($key)
	{
		Redis::incr($key);
	}

	public static function decr($key)
	{
		Redis::decr($key);
	}

	public static function exists($key)
	{
		Redis::exists($key);
	}

	//入栈
	public static function lpush($key,$val)
	{
		Redis::lpush($key,$val);
	}

	// 出栈
	public static function lpop($key)
	{
		return Redis::lpop($key);
	}

	public static function keys($key)
	{
		return Redis::keys($key.'*');
	}

	/**
	 * @param lng 127.909 ,lat 36.2323
	 */
	public static function geoadd($lng,$lat,$name)
	{
		return Redis::geoadd("fruit:shops:geo", $lng, $lat, $name);
	}

	public static function set_gifted_num($type='total')
	{
		self::incr('h5:giftedNum:'.$type);
	}

	public static function get_gifted_num($type='total')
	{
		return self::get('h5:giftedNum:'.$type);
	}

	public static function today_people_num() 
	{
		 $people_key = 'h5:drawtimes:'.date('Ymd').':';
		 return count(self::keys($people_key));
	}

	public static function gift_data($gift_id=1)
	{
		$key    = 'h5:gift:';
        $numKey = 'h5:gift:num:';
		$info = RedisService::get($key.$gift_id);
        $info = json_decode($info);
        $num = RedisService::get($numKey.$gift_id);
        return ($info->num - $num).'/'.$info->num;
	}

	public static function set_whitelist($openid)
	{
		$key = 'h5:whitelist:'.$openid;
		$rs = RedisService::set($key,'1');
		return $rs;
	}

	public static function get_whitelist($openid)
	{
		$key = 'h5:whitelist:'.$openid;
		$rs = RedisService::get($key);
		return $rs;
	}

	
		
}
?>